import AlgoPlus.CTPV6315 as ctp


@ctp.flexible_callback_wraps
def flexible_callback1(event_id, field, islast, error_id, error_msg, user_id):
    print("=======my flexible callback1 decode response by wraps")
    print(event_id, islast, user_id, error_id, error_msg)
    print(field)
    print("=======")


def flexible_callback2(event_id, field, islast, user_id, error_id, error_msg):
    print("=======my flexible callback2 decode response by function")
    print(event_id, islast, user_id, error_id, error_msg)
    field = ctp.decode_event_field(event_id, field)
    print(field)
    print("=======")


if __name__ == '__main__':

    print(f"||Version||,Version:{ctp.getVersion()}")

    login_field = ctp.CAPLoginField()
    login_field.UserType = ctp.ENUM_UserType_SIMNOWFuture
    login_field.UserID = ""
    login_field.Password = ""

    # Simnow仿真交易环境（http://simnow.com.cn）
    login_field.BrokerID = "9999"
    login_field.TraderFrontAddress = "tcp://180.168.146.187:10201"
    login_field.MdFrontAddress = "tcp://180.168.146.187:10211"
    login_field.AppID = "simnow_client_test"
    login_field.AuthCode = "0000000000000000"

    # # NSight期货仿真交易环境（http://n-sight.com.cn）
    # login_field.BrokerID = "10010"
    # login_field.TraderFrontAddress = "tcp://210.14.72.15:4600"
    # login_field.MdFrontAddress = "tcp://210.14.72.15:4602"
    # login_field.AppID = ""
    # login_field.AuthCode = ""

    trader = ctp.init(1, login_field, None, flexible_callback1)
    # trader = ap.init(1, login_field, None, flexible_callback2)

    if trader is not None:
        print("||init||,Succeed")
        ctp.loop()
    else:
        error_id = ctp.getInitError()
        message = ctp.getMessageByErrorID(error_id)
        print(f"||init||,ErrorID:{error_id},Message:{message}")
